package mwa.cs.admin.ctrl;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import mwa.cs.dao.SitesDAO;
import mwa.cs.dao.UserLoginDAO;
import mwa.cs.database.Database;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

/**
 * Servlet implementation class CS83003Srvl
 */
public class CS83003Srvl extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Database db;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CS83003Srvl() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}
	
	public void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		String vaProcessType = "";
    	vaProcessType =   request.getParameter("process_type");
//		System.out.println("vaProcessType : "+vaProcessType);
		if(vaProcessType=="getAllSite"||vaProcessType.equals("getAllSite")){
			getAllSite(request,response);
		}
				
	}
	private void getAllSite(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8"); 
		response.setContentType("text/JSON");
		
    	String[] vaMessage = null, vaValue = null;
    	this.db = new Database();
    	
    	SitesDAO voSitesDAO = new SitesDAO(db);
		List<Map<String, Object>> voResult = (List<Map<String, Object>>) voSitesDAO.findAll();
		db.close();
		
		vaValue = new String[voResult.size()];
		vaMessage = new String[voResult.size()];
			for (int i = 0; i < voResult.size(); i++) {
				Map<String, Object> voObject = voResult.get(i);	
				
				vaValue[i] =    String.valueOf(voObject.get("SITE_ID"));
				vaMessage[i] = (String) voObject.get("SITE_NAME");	
			}
    	
		
		
		JSONObject voObject = new JSONObject();
		JSONArray voList = new JSONArray();
		PrintWriter out = response.getWriter();
		if (vaMessage != null) {
			for (int i = 0; i < vaMessage.length; i ++) {
				voList.add(vaMessage[i]);
			}
		}
		voObject.put("messages", voList);
		JSONArray voList2 = new JSONArray();
		if (vaValue != null) {
			for (int i = 0; i < vaValue.length; i ++) {
				voList2.add(vaValue[i]);
			}
		}
		voObject.put("values", voList2);
		
		out.print(voObject.toJSONString());
		
	}

}
